package gogogo.liwushuo.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.CgLeftCheckAll_1;
import gogogo.liwushuo.activity.CgLeftCheckAll_2;
import gogogo.liwushuo.activity.CgleftBottomImageItem;
import gogogo.liwushuo.activity.FirstBannerActivity;
import gogogo.liwushuo.activity.HomeTitleActivity;
import gogogo.liwushuo.bean.CgLeftBottomBean;
import gogogo.liwushuo.bean.CgLeftHeaderBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.CommonAdapter;
import gogogo.liwushuo.util.MyHolder;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class CgLeftFragment extends Fragment {

    @InjectView(R.id.rv_category_left)
    RecyclerView mRecyclerView;
    @InjectView(R.id.rv_cgleft_header)
    RecyclerView mRecycleViewTop;
    private Activity mActivity;
    private LinearLayoutManager mLinearLayoutManager;
    private RecyclerViewHeader mRvHeader;
    private CommonAdapter<CgLeftHeaderBean.DataBean.ColumnsBean> mRvTopAdapter;
    private CgLeftHeaderBean mTopBean;
    private CgLeftBottomBean mBottomBean;
    private CommonAdapter<CgLeftBottomBean.GroupBean> mRvBottomAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cgleft, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        mRvHeader = (RecyclerViewHeader) view.findViewById(R.id.header);


        loadBottomDatas();
        loadHeaderDatas();
    }

    private void loadHeaderDatas() {
        OKHttpUtils.newInstance(mActivity).getAsyncMainData(Resource.URL_CATEGORY_BANNERS, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                mTopBean = new Gson().fromJson(result, CgLeftHeaderBean.class);
                mRvTopAdapter = new CommonAdapter<CgLeftHeaderBean.DataBean.ColumnsBean>
                        (mActivity, mTopBean.getData().getColumns(), R.layout.item_cgleft_header) {
                    @Override
                    public void bindData(MyHolder holder, int position, CgLeftHeaderBean.DataBean.ColumnsBean bean) {
                        holder.displayImage(bean.getBanner_image_url(), R.id.iv_cgleft_header_item)
                                .setText(bean.getTitle(), R.id.tv_cgleft_header_item_title)
                                .setText(bean.getSubtitle(), R.id.tv_cgleft_header_item_subtitle)
                                .setText(bean.getAuthor(), R.id.tv_cgleft_header_item_name);
                        setHeaderListener(holder,position,bean);
                    }
                };
                mRecycleViewTop.setAdapter(mRvTopAdapter);
                mRecycleViewTop.setLayoutManager(new GridLayoutManager(mActivity, 3, LinearLayoutManager.HORIZONTAL, false));
                mRvTopAdapter.notifyDataSetChanged();
            }
        });
    }

    private void loadBottomDatas() {
        OKHttpUtils.newInstance(mActivity).getAsyncMainData(Resource.URL_CATEGORY_FIRST, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                mBottomBean = new Gson().fromJson(result, CgLeftBottomBean.class);
                mRvBottomAdapter = new CommonAdapter<CgLeftBottomBean.GroupBean>
                        (mActivity,mBottomBean.data.channel_groups,R.layout.item_cgleft_bottom) {
                    @Override
                    public void bindData(MyHolder holder, int position, CgLeftBottomBean.GroupBean bean) {
                        holder.setText(bean.name,R.id.tv_item_bottom_title)
                                .displayImage(bean.channels.get(0).cover_image_url,R.id.iv_1)
                                .displayImage(bean.channels.get(1).cover_image_url,R.id.iv_2)
                                .displayImage(bean.channels.get(2).cover_image_url,R.id.iv_3)
                                .displayImage(bean.channels.get(3).cover_image_url,R.id.iv_4)
                                .displayImage(bean.channels.get(4).cover_image_url,R.id.iv_5)
                                .displayImage(bean.channels.get(5).cover_image_url,R.id.iv_6);

                        setBottomListener(holder,position,bean);
                    }
                };
                mLinearLayoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
                mRecyclerView.setAdapter(mRvBottomAdapter);
                mRecyclerView.setLayoutManager(mLinearLayoutManager);
                mRvHeader.attachTo(mRecyclerView);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick(R.id.tv_cgleft_header_checkmore)
    public void onClick() {
        startActivity(new Intent(mActivity, CgLeftCheckAll_1.class).putExtra("program", Resource.URL_CATEGORY_PROGRAM_CHECKALL));
    }

    private void setHeaderListener(MyHolder holder, int position, final CgLeftHeaderBean.DataBean.ColumnsBean bean) {
        View itemview = holder.getViewById(R.id.ll_itemview_cgleft_header);
        itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,HomeTitleActivity.class);
                intent.putExtra("id",bean.getId());
                intent.putExtra("title",bean.getTitle());
                startActivity(intent);
            }
        });

    }

    private void setBottomListener(MyHolder holder, final int position, final CgLeftBottomBean.GroupBean bean) {

        final Bundle bundle = new Bundle();
        TextView tv_checkall = holder.getViewById(R.id.tv_check_all);
        tv_checkall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putString("url",Resource.URL_CATEGORY_FIRST);
                bundle.putInt("position",position);
                startActivity(new Intent(mActivity, CgLeftCheckAll_2.class).putExtras(bundle));
            }
        });
        ImageView iv_1 = holder.getViewById(R.id.iv_1);
        iv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(0).id));
            }
        });
        ImageView iv_2 = holder.getViewById(R.id.iv_2);
        iv_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(1).id));
            }
        });
        ImageView iv_3 = holder.getViewById(R.id.iv_3);
        iv_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(2).id));
            }
        });
        ImageView iv_4 = holder.getViewById(R.id.iv_4);
        iv_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(3).id));
            }
        });
        ImageView iv_5 = holder.getViewById(R.id.iv_5);
        iv_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(4).id));
            }
        });
        ImageView iv_6 = holder.getViewById(R.id.iv_6);
        iv_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, CgleftBottomImageItem.class).putExtra("id",bean.channels.get(5).id));
            }
        });
    }
}
